home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / V15N04.ZIP / WARPCA.ZIP / WCABSRC.ZIP / FILECHNG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  8.6 KB  |  333 lines

  1. // DFileChangeDlg -- Class implementation
  2. #include <assert.h>
  3. #include <cstring.h>
  4. #include <process.h>
  5. #include <dir.h>
  6. #include <classlib\arrays.h>
  7. #include <owl\owlpch.h>
  8. #include <owl\dialog.h>
  9. #include <owl\edit.h>
  10. #include <owl\static.h>
  11. #include <owl\button.h>
  12. #include <owl\textgadg.h>
  13. #include "resource.h"
  14. #include "filentry.h"
  15. #include "filetool.h"
  16. #include "filechng.h"
  17.  
  18. #define DID_OK      1
  19. #define DID_CANCEL  2
  20.  
  21. DEFINE_RESPONSE_TABLE1(DFileChangeDlg, TDialog)
  22.     EV_CHILD_NOTIFY(IDC_TO, EN_CHANGE, OnTargetBoxChange),
  23.     EV_CHILD_NOTIFY(DID_OK, BN_CLICKED, CmOk),
  24. END_RESPONSE_TABLE;
  25.  
  26. void DFileChangeDlg::SetupWindow()
  27. {
  28.     TDialog::SetupWindow();
  29.  
  30.     FILEENTRY *pFileEntry;
  31.  
  32.     string strCaption;
  33.  
  34.     SendDlgItemMsg(IDC_FROM, EM_SETTEXTLIMIT, 4096, 0);
  35.     SendDlgItemMsg(IDC_TO, EM_SETTEXTLIMIT, 4096, 0);
  36.  
  37.     // Load up our text.
  38.     switch(nCommand)
  39.     {
  40.         case CM_MOVE:  strCaption = "Move";
  41.                             WinEnableWindow(GetDlgItem(DID_OK), 0);
  42.                             break;
  43.  
  44.         case CM_COPY:  strCaption = "Copy";
  45.                             WinEnableWindow(GetDlgItem(DID_OK), 0);
  46.                             break;
  47.  
  48.         case CM_DELETE: strCaption = "Delete";
  49.                             {
  50.                                 SetDlgItemText(IDC_FROM_PROMPT, "Delete:");
  51.  
  52.                                 WinShowWindow(GetDlgItem(IDC_TO_PROMPT), FALSE);
  53.                                 WinShowWindow(GetDlgItem(IDC_TO), FALSE);
  54.                             }
  55.                             break;
  56.  
  57.         default: MessageBox("Bad command in DFileChangeDlg::SetupWindow()!", "Error", MB_ICONSTOP);
  58.     }
  59.  
  60.     SetWindowText(strCaption.c_str());
  61.  
  62.     SetDlgItemText(IDC_CURRENT_DIR, lpszCurrentDir);
  63.  
  64.     string strSourceFiles("");
  65.  
  66.     // Build list of files
  67.     for(int i = 0; i < SelectedFileList.GetItemsInContainer(); i++)
  68.     {
  69.         pFileEntry = SelectedFileList[i];
  70.  
  71.         if(pFileEntry)
  72.         {
  73.             if(strSourceFiles.length() > 0)
  74.                 strSourceFiles += ", ";
  75.             strSourceFiles += pFileEntry->szFileName;
  76.         }
  77.     }
  78.     SetDlgItemText(IDC_FROM, strSourceFiles.c_str());
  79. }
  80.  
  81. void DFileChangeDlg::OnTargetBoxChange()
  82. {
  83.     // Enable or disable OK button based on whether target box is empty.
  84.     int cc = WinQueryWindowTextLength(GetDlgItem(IDC_TO));
  85.  
  86.     WinEnableWindow(GetDlgItem(DID_OK), cc);
  87. }
  88.  
  89. void DFileChangeDlg::CmOk()
  90. {
  91.     int rc;
  92.  
  93.     char szFullTarget[256];
  94.     char szTargetDir[256];
  95.     char szTargetFile[256];
  96.  
  97.     string strTestDir;
  98.  
  99.     GetDlgItemText(IDC_TO, szFullTarget, 256);
  100.  
  101.     int nLen = strlen(szFullTarget);
  102.  
  103.     // Special case #1 -- For entries not ending in '\' add one and
  104.     // test if this is a directory.
  105.     if(nLen > 0 && szFullTarget[nLen - 1] != '\\')
  106.     {
  107.         rc = chdir(szFullTarget);
  108.         if(!rc)
  109.             // This is a directory, so use it with globals...
  110.             strcat(szFullTarget, "\\*.*");
  111.     }
  112.  
  113.     // Special case #2, always append '*.*' to directories ending in '\'
  114.     if(nLen > 0 && szFullTarget[nLen - 1] == '\\')
  115.         strcat(szFullTarget, "*.*");
  116.  
  117.     string strTargetDir = szFullTarget;
  118.     string strTemp;
  119.     szTargetFile[0] = 0;
  120.  
  121.     //strTargetDir.to_upper();  // Case-sensitive for HPFS!
  122.  
  123.     string strAll("*.*");
  124.     size_t pos = strTargetDir.find(strAll);
  125.  
  126.     if(pos != NPOS)
  127.         strTargetDir = strTargetDir.substr(0, pos - 1);
  128.     else
  129.     {
  130.         string strQuestion("?");
  131.         string strGlobalStar("*");
  132.         string strSlash("\\");
  133.         string strColon(":");
  134.  
  135.         // Check to see if any '\' is provided...
  136.         pos = strTargetDir.find_last_of(strSlash);
  137.  
  138.         if(pos == NPOS)
  139.             // Could be a colon without a slash...
  140.             pos = strTargetDir.find_last_of(strColon);
  141.         if(pos != NPOS)        {
  142.             // Then split directory and filename.
  143.             strTemp = strTargetDir.substr(pos + 1);
  144.             strcpy(szTargetFile, strTemp.c_str());
  145.             strTemp = strTargetDir.substr(0, pos);
  146.             strTargetDir = strTemp;
  147.         }
  148.         else
  149.         {
  150.             // Then it has to be just a file
  151.             pos = strTargetDir.find_last_of(strSlash);
  152.             strcpy(szTargetFile, strTargetDir.c_str());
  153.             strTargetDir = lpszCurrentDir;  // Source and target dir's. are the same.
  154.         }
  155.     }
  156.  
  157.     SetCursor(0, IDC_WAIT);
  158.  
  159.     SetDlgItemText(IDC_STATUS, "Computing total size...");
  160.  
  161.     int nCount = CollectAllFilesInSelections(lpszCurrentDir, szMask, ulShowAttrBits, lpszCurrentDir);
  162.     if(nCount == 0)
  163.     {
  164.         MessageBox("Select some files first!", "No Files Selected", MB_ICONEXCLAMATION);
  165.         CloseWindow(TRUE);
  166.         return;
  167.     }
  168.  
  169.     SetCursor(0, IDC_ARROW);
  170.  
  171.     SetDlgItemText(IDC_STATUS, "");
  172.  
  173.     static FILEOPPARAM FileOpParam;
  174.     FileOpParam.pStatus = NULL;
  175.     // Use our dialog box's status control to display status info.
  176.     FileOpParam.hwndStatus = GetDlgItem(IDC_STATUS);
  177.     FileOpParam.pParent = pGlobalParent;
  178.     FileOpParam.lpszSourceDir = lpszCurrentDir;
  179.     FileOpParam.lpszTargetDir = (LPSTR)strTargetDir.c_str();
  180.     FileOpParam.lpszTargetFile = szTargetFile;
  181.  
  182.     string strConfirm("Are you sure you want to ");
  183.  
  184.     string strPlural("");
  185.  
  186.     char szTotal[20];
  187.  
  188.     int bIsCopy = TRUE;
  189.  
  190.     itoa(nCount, szTotal, 10);
  191.  
  192.     if(nCount == 1)
  193.         strPlural = "";
  194.     else
  195.         strPlural = "s";
  196.  
  197.     FileOpParam.nFileOp = nCommand;
  198.  
  199.     // Load up our text.
  200.     switch(nCommand)
  201.     {
  202.         case CM_MOVE:  // Load files...
  203.         case CM_COPY:
  204.                             // Confirmation options.
  205.                             if(bConfirmCopy || (nCommand == CM_MOVE && bConfirmDelete) || bConfirmAll)
  206.                             {
  207.                                 bIsCopy = nCommand == CM_COPY ? TRUE : FALSE;
  208.                                 if(bIsCopy)
  209.                                     strConfirm += "copy ";
  210.                                 else
  211.                                     strConfirm += "move ";
  212.                                 strConfirm += szTotal;
  213.                                 strConfirm += " object";
  214.                                 strConfirm += strPlural;
  215.                                 strConfirm += " (";
  216.                                 strConfirm += GetFileSizeAsStr(ulTotalFilesBytes);
  217.                                 strConfirm += ") to ";
  218.                                 strConfirm += strTargetDir;
  219.                                 strConfirm += "?";
  220.                                 rc = MessageBox(strConfirm.c_str(),
  221.                                                  bIsCopy ? "Confirm Copy" : "Confirm Move",
  222.                                                  MB_YESNOCANCEL);
  223.                                 if(rc != IDYES)
  224.                                 {
  225.                                     CleanupArrays();
  226.                                     CloseWindow(FALSE);
  227.                                     return;
  228.                                 }
  229.                             }
  230.                             // Confirm new directory, if this is one.
  231.                             // For drive letters only, we can't create anything new!!!
  232.                             if(strTargetDir[1] == ':' && strTargetDir.length() == 2)
  233.                             {
  234.                                 strTestDir = strTargetDir + "\\";
  235.                                 rc = chdir((LPSTR)strTestDir.c_str());
  236.                                 if(rc)
  237.                                 {
  238.                                     strConfirm = "Invalid drive ";
  239.                                     strConfirm += strTargetDir;
  240.                                     MessageBox(strConfirm.c_str(), "Invalid Target Directory", MB_OK);
  241.                                     CleanupArrays();
  242.                                     CloseWindow(FALSE);
  243.                                     return;
  244.                                 }
  245.                             }
  246.                             else
  247.                             {
  248.                                 // If drive letter provided, use target path unchanged.
  249.                                 // Otherwise, use source path + target path.
  250.                                 if(strTargetDir[1] != ':')
  251.                                 {
  252.                                     strTemp = lpszCurrentDir;
  253.                                     strTemp += "\\";
  254.                                     strTemp += strTargetDir;
  255.                                     strTargetDir = strTemp;
  256.                                 }
  257.                                 // First, check that this directory doesn't already exist.  If not, confirm creating it.
  258.                                 rc = chdir((LPSTR)strTargetDir.c_str());
  259.                                 if(rc)
  260.                                 {
  261.                                     strConfirm = "Destination directory ";
  262.                                     strConfirm += strTargetDir;
  263.                                     strConfirm += " does not exist. Create?";
  264.                                     rc = MessageBox(strConfirm.c_str(), "Confirm Directory Create", MB_YESNOCANCEL);
  265.                                     if(rc != IDYES)
  266.                                     {
  267.                                         CleanupArrays();
  268.                                         CloseWindow(FALSE);
  269.                                         return;
  270.                                     }
  271.                                 }
  272.                             }
  273.                             //strTargetDir = strTestDir;
  274.                             FileOpParam.lpszTargetDir = (LPSTR)strTargetDir.c_str();
  275.  
  276.                             rc = CopyOrMoveFiles(&FileOpParam);
  277.                             if(!rc)
  278.                             {
  279.                                 if(bIsCopy)
  280.                                     MessageBox(bIsCopy ? "Can't copy files!" : "Can't move files!",
  281.                                                   "Copy Files Failed", MB_ICONSTOP);
  282.                                 CleanupArrays();
  283.                                 CloseWindow(FALSE);
  284.                             }
  285.                             else
  286.                             {
  287.                                 CleanupArrays();
  288.                                 CloseWindow(TRUE);
  289.                             }
  290.                             break;
  291.  
  292.         case CM_DELETE:
  293.                             if(bConfirmDelete || bConfirmAll)
  294.                             {
  295.                                 strConfirm += "delete ";
  296.                                 strConfirm += szTotal;
  297.                                 strConfirm += " object";
  298.                                 strConfirm += strPlural;
  299.                                 strConfirm += " (";
  300.                                 strConfirm += GetFileSizeAsStr(ulTotalFilesBytes);
  301.                                 strConfirm += ") from ";
  302.                                 strConfirm += strTargetDir;
  303.                                 strConfirm += "?  This action can't be undone.";
  304.                                 rc = MessageBox(strConfirm.c_str(), "Confirm Delete", MB_YESNOCANCEL);
  305.                                 if(rc != IDYES)
  306.                                 {
  307.                                     CloseWindow(FALSE);
  308.                                     return;
  309.                                 }
  310.                             }
  311.                             rc = DeleteFiles(&FileOpParam);
  312.                             if(!rc)
  313.                             {
  314.                                 MessageBox("Can't delete files!", "Delete Failed", MB_ICONSTOP);
  315.                                 CleanupArrays();
  316.                                 CloseWindow(FALSE);
  317.                                 return;
  318.                             }
  319.                             else
  320.                             {
  321.                                 CleanupArrays();
  322.                                 CloseWindow(TRUE);
  323.                             }
  324.                             break;
  325.  
  326.         default: MessageBox("Bad command in DFileChangeDlg::SetupWindow()!", "Error", MB_ICONSTOP);
  327.     }
  328.  
  329.     CloseWindow(TRUE);
  330. }
  331.  
  332.  
  333.